
/**
 * @author Administrator
 * 2008/08/25
 * */
package com.wsj.web.action.homeSiteNews;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.wsj.service.IBmClassTypeServ;
import com.wsj.service.IHomeSiteNewsClassServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.web.action.BaseAction;
import com.wsj.web.form.BmSite.HomeSiteNewsClassForm;
import com.wsj.web.vo.homeSite.BmClassType;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;

public class NewsClassAction extends BaseAction {
	

	private IHomeSiteNewsClassServ homeSiteNewsClassServ;
	
	private IBmClassTypeServ bmClassTypeServImpl;
	
	
	/* 添加分类 第一步： */
	public ActionForward toAdd(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		String topage = "add";
		this.setChildStr(request);
		List list = new ArrayList();  
	    list = this.bmClassTypeServImpl.loadAllBmClassType();
	    request.setAttribute("classTypeList", list);
		return mapping.findForward(topage);
	}
	
	/* 添加分类 第二步： */
	public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		String topage = "list_add";
		try {
			HomeSiteNewsClassForm hpForm = (HomeSiteNewsClassForm)form;
			
			HomeSiteNewsClass checkHomeSiteNewsClass = this.homeSiteNewsClassServ.loadNewsClassByNames(hpForm.getNames());
			if(checkHomeSiteNewsClass==null)
			{
				HomeSiteNewsClass hs = new HomeSiteNewsClass();
				BeanUtils.copyProperties(hs, hpForm);
				/* seq 处理 */
				this.setSeqString(hpForm, hs);
				/*  关联关系 */
				Integer classTypeId = hpForm.getClassTypeId();
				BmClassType bc = this.bmClassTypeServImpl.getBmClassTypeById(classTypeId);
				if(bc!=null){
					hs.setBmClassType(bc);
				}
				
				hs.setId(null);
				this.homeSiteNewsClassServ.addNewsClass(hs);
				request.setAttribute("msg", "<script>alert('添加成功!');</script>");
			}
			else
			{
				request.setAttribute("msg", "<script>alert('添加失败,名称重复!');</script>");
			}
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		// 获取分类树图
		this.setChildStr(request);
		return mapping.findForward(topage);
	}
	
	/* 删除分类 第一步： */
	public ActionForward toDel(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		String topage = "delete";
		this.setChildStr(request);
		return mapping.findForward(topage);
	}
	
	/* 删除分类 第二步： */
	public ActionForward delete(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
		String topage = "list_del";
		try {
			HomeSiteNewsClassForm hpForm = (HomeSiteNewsClassForm)form;
			Long id = hpForm.getId();
			if(id!=null && !id.equals(0) && !id.equals(-1)){
				this.homeSiteNewsClassServ.deleteNewsClass(id);
				request.setAttribute("msg", "<script>alert('删除成功!');</script>");
			}
			// 获取分类树图
			this.setChildStr(request);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return mapping.findForward(topage);
	}
	
	/* 修改分类 第一步： */
	public ActionForward toUpdate(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		String topage = "update";
		// 获取分类树图
		this.setChildStr(request);
		return mapping.findForward(topage);
	}
	
	/* 修改分类 第二步： */
	public ActionForward update(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
		String topage = "list_update";
		try {
			HomeSiteNewsClassForm hpForm = (HomeSiteNewsClassForm)form;
			// 获取分类树图
			this.setChildStr(request);
			List list = new ArrayList();  
		    list = this.bmClassTypeServImpl.loadAllBmClassType();
		    request.setAttribute("classTypeList", list);
			HomeSiteNewsClass hp = this.homeSiteNewsClassServ.loadNewsClassById(hpForm.getId());
			request.setAttribute("homeSiteNewsClass", hp);
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		return mapping.findForward(topage);
	}
	
	/* 修改分类 第三步： */
	public ActionForward updateDo(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		String topage = "list_update";
		try {
			HomeSiteNewsClassForm hpForm = (HomeSiteNewsClassForm)form;
			HomeSiteNewsClass hs = new HomeSiteNewsClass();
			Long id = hpForm.getId();
			if(id!=null && !id.equals(0) && !id.equals(-1)){
				hs = this.homeSiteNewsClassServ.loadNewsClassById(id);
			}
			hpForm.setSeq(hs.getSeq());
			BeanUtils.copyProperties(hs, hpForm);
			
			/*  关联关系 */
			Integer classTypeId = hpForm.getClassTypeId();
			BmClassType bc = this.bmClassTypeServImpl.getBmClassTypeById(classTypeId);
			if(bc!=null){
				hs.setBmClassType(bc);
			}
			
			this.homeSiteNewsClassServ.updateNewsClass(hs);
			request.setAttribute("msg", "<script>alert('修改成功!');</script>");
			// 获取分类树图
			this.setChildStr(request);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return mapping.findForward(topage);
	}
	
	/* 迁移分类树图 第一步 */
	public ActionForward toMove(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws BusinessException{
		String topage = "move";
		// 获取分类树图
		this.setChildStr(request);
		return mapping.findForward(topage);
	}
	
	/* 迁移分类树图 第二步 */
	public ActionForward move(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
		String topage = "list_move";
		HomeSiteNewsClassForm hpForm = (HomeSiteNewsClassForm)form;
		HomeSiteNewsClass hs;
		boolean flag = false;
		try {
			hs = this.homeSiteNewsClassServ.loadNewsClassById(hpForm.getId());
			hs.setParenId(hpForm.getParenId());
			// 调用存储过程对属性 seq 的处理 
			flag = this.homeSiteNewsClassServ.updateSeq(hpForm.getId(),hpForm.getParenId());
			/*if(flag){
				this.homeSiteNewsClassServ.updateNewsClass(hs);
				request.setAttribute("msg", "<script>alert('迁移成功!');</script>");
			}else{
				request.setAttribute("msg", "<script>alert('迁移失败!');</script>");
			}*/
			if(flag){
				this.homeSiteNewsClassServ.updateNewsClass(hs);
				request.setAttribute("msg", "<script>alert('迁移成功!');</script>");
			}
			
		} catch (BusinessException e) {
			e.printStackTrace();
		}
		
		
		return mapping.findForward(topage);
	}
	
	/* 分类树图 搜索 */
	public ActionForward search(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
		String topage = "list";
		
		try {
			this.setChildStr(request);
		} catch (BusinessException e) {
			this.getLogger().error("获取所有类目列表有误",e);
			topage = "error";
			request.setAttribute("error", "获取类目列表有误");
		}
		
		return mapping.findForward(topage);
	}
	

	/**
	 *  获取 新闻分类的视图  
	 *  将重复的代码封装成 方法，没别的用途 
	 * */
	private void setChildStr(HttpServletRequest request) throws BusinessException{
		Long rootId = new Long(-1);
		String str = this.homeSiteNewsClassServ.getChildStr(rootId,null,0, -1);
		request.setAttribute("newsClassTree_right", str);
		String proStr = str.replace("</option>", "<br>");
		proStr = proStr.replaceAll("【", "【<font color=red>");
		proStr = proStr.replaceAll("】", "</font>】");
		request.setAttribute("newsClassTree_left", proStr); 
	}
	
	/**
	 *  对属性 seq 的处理  
	 *  将重复的代码封装成 方法，没别的用途 
	 * @throws BusinessException 
	 * */
	private void setSeqString (HomeSiteNewsClassForm form ,HomeSiteNewsClass hs) throws BusinessException{
		String strSeq = this.homeSiteNewsClassServ.getSeq(form.getId(),form.getParenId());
		hs.setSeq(strSeq);
	}

	public IHomeSiteNewsClassServ getHomeSiteNewsClassServ() {
		return homeSiteNewsClassServ;
	}

	public void setHomeSiteNewsClassServ(
			IHomeSiteNewsClassServ homeSiteNewsClassServ) {
		this.homeSiteNewsClassServ = homeSiteNewsClassServ;
	}
	

	public IBmClassTypeServ getBmClassTypeServImpl() {
		return bmClassTypeServImpl;
	}


	public void setBmClassTypeServImpl(IBmClassTypeServ bmClassTypeServImpl) {
		this.bmClassTypeServImpl = bmClassTypeServImpl;
	}

	
}
